<?php


namespace Home\Controller;

//use Home\DAO\TaobaoTrades;
ini_set('max_execution_time', '0');     //设置为0，就是不限制执行的时间。
use Home\Service\TobaoService;
Vendor('Taobao.TopSdk');

class TaobaoorderController
{
    protected function stdClass_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = self::stdClass_array($value);
            }
        }
        return $array;
    }
    public function auth()
    {
//        require __DIR__ . '/../Common/taobao_php/Autoloader.php';
//        require __DIR__ . '/../Common/taobao_php/top/TopClient.php';
//        require __DIR__ . '/../Common/taobao_php/top/request/TradesSoldIncrementvGetRequest.php';
        $c = new \TopClient;
        $c->appkey = '32974608';
        $c->secretKey = 'e10ac4e66a3efb57e71a33fe82fab787';
        $sessionKey = '610251788e398d2ebbd1a0e041f829d3e832ca5888cb0ec2209208744723';
        $req = new \TradesSoldIncrementvGetRequest;
        $req->setFields("tid,type,status,payment,orders");
//        $req->setStartCreate("2000-01-01 00:00:00");
//        $req->setEndCreate("2000-01-02 00:00:00");
//        $req->setStatus("TRADE_NO_CREATE_PAY");
//        $req->setType("fixed");
//        $req->setExtType("service");
//        $req->setTag("time_card");
        $req->setStartCreate("2021-08-06 00:00:00");
        $req->setEndCreate("2021-08-07 00:00:00");

//        $req->setUseHasNext("true");
        $req->setPageNo("1");
        $req->setPageSize("40");
        $req->setUseHasNext("true");
        $resp = $c->execute($req, $sessionKey);
        var_dump($resp);
    }

    public function sold()
    {
        $c = new \TopClient;
        $c->appkey = '32974608';
        $c->secretKey = 'e10ac4e66a3efb57e71a33fe82fab787';
        $page_no = 1;
        $c->format = 'json';
        $c->simplify = 'true';
        $sessionKey = '610251788e398d2ebbd1a0e041f829d3e832ca5888cb0ec2209208744723';
        $req = new \TradesSoldGetRequest;
        $req->setPageNo($page_no);
        $req->setPageSize(100);  //Money
        $req->setUseHasNext("true");
        $req->setFields("tid,type,status,payment,orders,receiver_mobile,receiver_phone,receiver_name,buyer_nick,has_buyer_message,o2o_guide_id,o2o_guide_name,end_time,created,pay_time,modified");
//        $req->setStartCreated("2021-07-20 00:00:00");
//        $req->setEndCreated("2021-08-06 12:00:00");
        $req->setStartCreated(date("Y-m-d H:i:s", strtotime("-3 month")));
        $req->setEndCreated(date("Y-m-d H:i:s"));
        $resp = $c->execute($req, $sessionKey);
        $data = self::stdClass_array($resp);
//        var_dump($data);
//        var_dump($resp->has_next);
//        die();
//        var_dump($resp->trades->trade);

//        var_dump($data['has_next']);
//        if ($data['has_next'] == true)
//        {
//            $c->page_no = ++$page_no;
//            var_dump($c->page_no);
//            $resp = $c->execute($req, $sessionKey);
//            $temp = self::stdClass_array($resp);
//            $data = array_merge($data,$temp);
//        }
        while ($data['has_next'] == true){
            $req->setPageNo(++$page_no);
            var_dump($page_no);
            $resp = $c->execute($req, $sessionKey);
            $temp = self::stdClass_array($resp);
            $data['has_next'] =$temp['has_next'];
//            $data['trades']['trade'][] = $temp['trades']['trade'];
            $data['trades']['trade'] = array_merge($data['trades']['trade'],$temp['trades']['trade']);
//            echo $data['has_next'];
        }
//        var_dump($data);
        return $data;
    }
    public function savesold()
    {
//        $db = $this->db();
//        $log = null;
        $dao = new TobaoService();
        $resp = self::sold();
        $dao->saveorder($resp['trades']['trade']);
//        foreach ($resp['trades']['trade'] as $value){
//
//
//        }

    }
    public function display()
    {

    }

}